import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../../pages/security/securitycheck_end.dart';
import '../../model/security_check.dart';
import '../../pages/security/security_helper.dart';
import '../../pages/security/securitycheck_add.dart';
import '../../helper/channelHelper.dart';
import '../../helper/routeHelper.dart';
import '../../widgets/IconWidget.dart';
import '../../widgets/inputWidget.dart';
import '../../helper/Helper.dart';
import '../../helper/uidata.dart';

class SecurityCheckInfoPage extends StatefulWidget {
  @override
  SecurityCheckInfoPage(this.stc1, this.added);
  final SecurityCheck stc1;
  final VoidCallback added;
  @override
  SecurityCheckInfoPageState createState() => new SecurityCheckInfoPageState();
}

class SecurityCheckInfoPageState extends State<SecurityCheckInfoPage> {
  late SecurityCheck X1;
  bool ifLoaded = false;

  @override
  void initState() {
    super.initState();
    X1 = widget.stc1;
    loadData();
  }

  loadData() async {
    //ifLoaded=false;
    var xx = await SecurityCheck.GetByID(widget.stc1.ID);
    if (xx!.ExamQM == null) xx.ExamQM = "";

    setState(() {
      ifLoaded = true;
      X1 = xx;
    });
  }

  upload(BuildContext context) async {
    if (X1.ID == 0) {
      await SecurityCheck.Add(X1);
    } else {
      await SecurityCheck.Update(X1);
    }
    // widget.added();
    Navigator.of(context).pop();
  }

  void onEQMChange(String nn) async {
    String yy = nn.toString();

    //print(yy);
    setState(() {
      X1.ExamQM = yy;
    });
    await SecurityCheck.Update(X1);
  }

  Widget? getFloatingButton(BuildContext context) {
    if (X1.ExamQM == null) X1.ExamQM = "";

    if (X1.ExamQM == "") {
      if (Helper.UserPB(widget.stc1.UserId)) {
        if (X1.State == 0)
          return FloatingActionButton(
              child: Icon(FontAwesomeIcons.penAlt),
              onPressed: () => RouteHelper.ShowPage(
                  context, SecurityCheckAddPage(widget.stc1, loadData)));

        if (X1.State == 1)
          return FloatingActionButton(
              child: Icon(FontAwesomeIcons.penAlt),
              onPressed: () => RouteHelper.ShowPage(
                  context, SecurityCheckEndPage(widget.stc1, loadData)));
      } else {
        return Container();
      }
    }

    // return FloatingActionButton(
    //     child: Icon(FontAwesomeIcons.download),
    //     onPressed: () async {
    //       //await  DownloadHelper.DownLoadSecurityCheck(context, widget.stc1);
    //       await ChannelHelper.showToast("已下载，请到下载列表里查看");
    //     });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,

      // appBar: AppBar(
      //   title: Text("安全检查"),
      // ),
      body: ifLoaded == false
          ? Center(
              child: SpinKitCircle(
              color: Colors.blue,
            ))
          : SecurityCheckInfoBody(context),
      floatingActionButton: getFloatingButton(context),
    );
  }

  List<Widget> getAllWidget(BuildContext context) {
    if (X1.ExamQM == null) X1.ExamQM = "";

    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];

    list1.add(InputWidget.inCard(
      ListTile(
        leading: IconWidget.CheckImage(),
        title: Text(X1.Title),
        subtitle: Text(Helper.DateTimeFormat(X1.CreateTime)),
      ),
    ));

    //list2.add(InputWidget.getInfo("设备名称",Helper.DateTimeFormat(X1.CreateTime)));
    //list2.add(Divider(height: 0.0,));
    list2.add(InputWidget.getInfo("检查类型", X1.SType));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInfo("检查区域", X1.Region));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInfo2("检查项目", X1.CheckItems));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInfo("检查人员", X1.Members));

    list3.add(InputWidget.getInfo2("检查内容", X1.Content));
    list3.add(Divider(
      height: 0.0,
    ));
    list3.add(InputWidget.getInfo2("发现问题", X1.Problems));
    list3.add(Divider(
      height: 0.0,
    ));
    list3.add(InputWidget.getInfo2("处置措施", X1.Works));
    list3.add(Divider(
      height: 0.0,
    ));
    list3.add(InputWidget.getInfo2("备注", X1.Remark));

    list1.add(InputWidget.inCard2(list2));

    if (X1.State > 0) {
      list1.add(InputWidget.inCard2(list3));

      if (!Helper.UserPB(widget.stc1.UserId) &&
          SecurityHelper.IfGuanLi(UIData.CurrentUser!) &&
          X1.ExamQM == "") {
        list4.add(
            InputWidget.getQMInput(context, "审核签字", X1.ExamQM, onEQMChange));
      } else {
        list4.add(InputWidget.getQMTile3("审核", X1.ExamQM));
      }

      list1.add(InputWidget.inCard2(list4));
    } else {
      list1.add(InputWidget.inCard(InputWidget.getButton(
          "填写检查报表",
          () => RouteHelper.ShowPage(
              context, SecurityCheckEndPage(X1, loadData)))));
    }
    return list1;
  }

  SecurityCheckInfoBody(BuildContext context) => SingleChildScrollView(
        child: SecurityCheckInfoFields(context),
      );

  SecurityCheckInfoFields(BuildContext context) {
    return SingleChildScrollView(
      child: new Container(
          margin: EdgeInsets.only(top: 4.0),
          child: new Column(children: getAllWidget(context)
              /*
            Card(
              child: (ListTile(
                title: Center(child: Text("查看地图")),
                onTap: () {
                  RouteHelper.MyMapPage(context, s1.LTTD, s1.LGTD);
                },
              )),
              color: GlobalConfig.cardBackgroundColor,
              margin: const EdgeInsets.all(6.0),
            ),*/
              )),
    );
  }
}
